package com.kimho.rollback.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.kimho.core.PageInfo;
import com.kimho.inhouse.domain.Inhouse;
import com.kimho.rollback.domain.Rollback;
import com.kimho.rollback.service.RollbackService;
import com.kimho.util.Util;
@Controller
@RequestMapping("/rollback")
public class RollbackController {

	@Autowired
	private RollbackService rollbackService;
	/**
	 * 登录验证
	 * @param request
	 * @param response
	 * @param user
	 * @return
	 */
	@RequestMapping(value="/items",method=RequestMethod.POST)
	public @ResponseBody Map<String,Object> getRollbackList_t(HttpServletRequest request,   
            HttpServletResponse response){
		Map<String,Object> responseMap = new HashMap<String,Object>();
		PageInfo pi = new PageInfo(request);
		Map<String,Object> param = new HashMap<String, Object>();
		param.put("start", pi.getStart());
		param.put("limit", pi.getLimit());
		param.put("query", request.getParameter("query"));
		param.put("logistics_id", request.getParameter("logistics_id"));
		param.put("notin", request.getParameter("notin"));//取出没有加入物流单号的标示
		List<Rollback> rollback = rollbackService.getRollbackList(param);
		responseMap.put("totalCount", rollbackService.getTotal(param));
		responseMap.put("rows", rollback);
		return responseMap;
	}
	
	@RequestMapping(value="/manager",method=RequestMethod.POST)
	public @ResponseBody Map<String,Object> managerdeal(HttpServletRequest request,   
            HttpServletResponse response){
		Map<String,Object> responseMap = new HashMap<String,Object>();
		String staff_id = request.getParameter("staff_id");
		String cost_brief = request.getParameter("cost_brief");
		String object_id = request.getParameter("object_id");
		String objid = request.getParameter("objid");
		String local_id = request.getParameter("local_id");
		String t_traff_fee =request.getParameter("t_traff_fee");
		String t_mid_fee=request.getParameter("t_mid_fee");
		String t_saf_fee=request.getParameter("t_saf_fee");
		String t_inst_fee=request.getParameter("t_inst_fee");
		String t_oth_fee=request.getParameter("t_oth_fee");
		String cost_type=request.getParameter("cost_type");
		String cost_id = request.getParameter("cost_id");
		String transitf	= request.getParameter("transitf");
		String transit_id=request.getParameter("transit_id");
		String settle = request.getParameter("settle");
		String returnf=request.getParameter("returnf");
		String obj_num = request.getParameter("obj_num");
		String traff_fee=request.getParameter("traff_fee");
		String saf_fee=request.getParameter("saf_fee");
		String t_pre_fee=request.getParameter("t_pre_fee");
		try {
		{
			if("1".equals(returnf))
			{
				Rollback rollback=new Rollback();
				rollback.setSaf_fee(Double.parseDouble(saf_fee));
				rollback.setTraff_fee(Double.parseDouble(traff_fee));
				rollback.setObject_id(object_id);
				rollback.setObj_num(obj_num);
				rollbackService.returnback(rollback);
			//	Rollback rollback = new Rollback();
				String newid = Util.getUUID();
				rollback.setOver("3");//订单回首
				rollback.setCost_id(cost_id);
				rollback.setT_traff_fee(Double.parseDouble(t_traff_fee));
				rollback.setT_mid_fee(Double.parseDouble(t_mid_fee));
				rollback.setT_saf_fee(Double.parseDouble(t_saf_fee));
				rollback.setT_inst_fee(Double.parseDouble(t_inst_fee));
				rollback.setT_oth_fee(Double.parseDouble(t_oth_fee));
				rollback.setCost_type(cost_type);
				rollback.setCost_id(cost_id);
				rollback.setSettle(settle);
				rollback.setT_pre_fee(Double.parseDouble(t_pre_fee));
				 if( "1".equals(transitf))
			    {
					 rollback.setTransitf("中转货");
			    }
			    else
			    {
			    	rollback.setTransitf("本公司");
			    }
				 rollback.setTransit_id(transit_id);
				 rollback.setTransit_det_id(newid);
				rollbackService.updateRollback(rollback);
				responseMap.put("success", "true");
				responseMap.put("info", "新增成功！");
			}
			else
			{
				Rollback rollback = new Rollback();
				String newid = Util.getUUID();
				rollback.setObj_num(obj_num);
				rollback.setOver("0");//订单回首
				rollback.setCost_id(cost_id);
				rollback.setT_traff_fee(Double.parseDouble(t_traff_fee));
				rollback.setT_mid_fee(Double.parseDouble(t_mid_fee));
				rollback.setT_saf_fee(Double.parseDouble(t_saf_fee));
				rollback.setT_inst_fee(Double.parseDouble(t_inst_fee));
				rollback.setT_oth_fee(Double.parseDouble(t_oth_fee));
				rollback.setT_pre_fee(Double.parseDouble(t_pre_fee));
				rollback.setCost_type(cost_type);
				rollback.setObject_id(object_id);
				rollback.setCost_id(cost_id);
				rollback.setSettle(settle);
				 if( "1".equals(transitf))
			    {
					 rollback.setTransitf("中转货");
			    }
			    else
			    {
			    	rollback.setTransitf("本公司");
			    }
				 rollback.setTransit_id(transit_id);
				 rollback.setTransit_det_id(newid);
				rollbackService.updateRollback(rollback);
				responseMap.put("success", "true");
				responseMap.put("info", "新增成功！");
			}
		}
			return responseMap;
		}catch(Exception e) {
			e.printStackTrace();
			responseMap.put("success", "false");
			responseMap.put("info", e.getClass()+":"+e.getMessage());
			return responseMap;
		}
	}

}
